using System;
using System.Collections.Generic;
using System.Text;
using Xyne.Utilities;
using GarageGames.Torque.Core;
using Microsoft.Xna.Framework;
using GarageGames.Torque.T2D;
using Xyne.Templates.Ui.BattleRegion.Bars;
using Xyne.Actions;
using Xyne.Regions;

namespace Xyne.Templates.BattleEffects
{
    public class RepeatBurnEffect : BattleEffect
    {
        #region Constants
        private const string ObjectName = "RepeatBurnEffect";
        #endregion

        #region Members
        private int _numTimes;
        private RepeatHealthAction _repeatHealthAction;
        private BattleRegionImpl _battleRegion;
        private ActionTargets _target;
        #endregion

        #region Template
        private static RepeatBurnEffect _template;

        public static RepeatBurnEffect Template
        {
            get
            {
                if (_template == null)
                {
                    _template = new RepeatBurnEffect();
                }
                return _template;
            }

        }
        #endregion

        #region Constructor
        public RepeatBurnEffect()
        {
            IsTemplate = true;
            Name = ObjectName;

            CreateWithWorldLimit = true;
            AnimationData = AnimationManager.Instance.GetAnimationData("burnANIM");           
            Layer = Game.EffectLevel;
            ObjectType = TorqueObjectDatabase.Instance.GetObjectType(Name);

            Size = new Vector2(30, 30);
        }

        public void Init(RepeatHealthAction repeatHealthAction, BattleRegionImpl battleRegion, ActionTargets target)
        {
            AnimationData.AnimationDuration = repeatHealthAction.SecondsPerRepeat;
            _repeatHealthAction = repeatHealthAction;
            _numTimes = _repeatHealthAction.NumTimesRepeat;
            _battleRegion = battleRegion;
            _target = target;
        }
        #endregion

        #region Overrides
        public override void StartEffect()
        {
            base.StartEffect();
            OnAnimationEnd = new OnAnimationEndDelegate(AnimationEnded);
        }
        #endregion

        #region Events
        public void AnimationEnded()
        {
            if (_numTimes == 0)
            {
                BattleEffectManager.Instance.RemoveBattleEffect(this);
                MarkForDelete = true;
            }
            else
            {
                _battleRegion.ModifyHealth(_target, _repeatHealthAction.HealthEffect);
                SetAnimationFrame(0);
                ResumeAnimation();
                _numTimes--;
            }
        }
        #endregion
    }
}
